
module.exports = {

  //post请求
  post: function (url, data, successCallback, failCallback) {
    var fetchOptions = {
      method: 'POST',
      headers: {
        //'Accept': 'application/json',
        //"Content-Type": "text/plain",
        'Content-Type': 'application/json'
      },
      mode:'cors',
      body: JSON.stringify(data)
    };

    fetch(url, fetchOptions)
    .then((response) => response.text())
    .then((responseText) => {
      successCallback(JSON.parse(responseText));
    })
    .catch(function(err){
        if(failCallback!=undefined){
          failCallback(err);
        }else{
          alert(err);
        }

      });
  },
  //get请求
  get: function(url, successCallback, failCallback){
    var token = window.localStorage.getItem('token');
    var fetchOptions = {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'x-access-token':token
      },
      mode:'cors',
    };
    fetch(url,fetchOptions)
      .then((response) => response.text())
      .then((responseText) => {
        successCallback(JSON.parse(responseText));
      })
      .catch(function(err){
        if(failCallback!=undefined){
          failCallback(err);
        }else{
          alert(err);
        }
      });
  },
  //delete请求
  delete: function(url, successCallback, failCallback){
    var fetchOptions = {
      method: 'delete',
      mode:'cors',
    };
    fetch(url,fetchOptions)
      .then((response) => response.text())
      .then((responseText) => {
        successCallback(JSON.parse(responseText));
      })
      .catch(function(err){
        if(failCallback!=undefined){
          failCallback(err);
        }else{
          alert(err);
        }
      });
  },
};
